package com.hlmc.controller;
import com.hlmc.entity.Banner;
import com.hlmc.entity.Page;
import com.hlmc.entity.VideoCover;
import com.hlmc.entity.Zy2;
import com.hlmc.service.Query_360_Service;
import com.hlmc.service.Zy2Service;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.*;

@Controller
public class Zy2Controller {

    @Resource
    private Zy2Service zy2Service;
    @Resource
    private Query_360_Service query_360;
    @ResponseBody
    @RequestMapping("/v_zy2")
    public List<Zy2> videoPage(String title){

        title.replace(" ","");
        if(title.equals("")){
            title=null;
        }
        Map<String,String> map=new HashMap<String, String>();
        map.put("title",title);
       List<Zy2> list=new ArrayList<Zy2>();
       list=zy2Service.findZy2(map);
       return list;
    }
    @ResponseBody
    @RequestMapping("/zy2_category2")
    public List<Zy2> FindByCategory2(String category, @RequestParam(defaultValue = "1") int page){
        Map<String,Object> map=new HashMap<String, Object>();
        int pageSize=18;
        category=category.replace(" ","").trim();
        int totalRecord=zy2Service.findTotalNum(category);
        Page<Zy2> coverPage = new Page<Zy2>(pageSize, page, totalRecord, null);
        int start = (coverPage.getCurrentPage() - 1) * pageSize;
        map.put("start", start);
        map.put("size", pageSize);
        map.put("category",category);
        List<Zy2> list=new ArrayList<Zy2>();
        list=zy2Service.findZy2ByCategory(map);
        return list;
    }

    @RequestMapping("/v_zy2_2")
    public String videoPage2(String title, Model model){
        List<Banner> banners=new ArrayList<Banner>();
        banners=query_360.findBner();
        title=title.replace(" ","").trim();
        Map<String,String> map=new HashMap<String, String>();
        map.put("title",title);
        List<Zy2> list=new ArrayList<Zy2>();
        list=zy2Service.findZy2(map);
        model.addAttribute("listCover",list);
        model.addAttribute("baseurl","3");
        model.addAttribute("banners",banners);
        return "index";
    }

    @RequestMapping("/zy2_category")
    public String FindByCategory(String category, Model model,@RequestParam(defaultValue = "1") int page){
        Map<String,Object> map=new HashMap<String, Object>();
        int pageSize=24;
        category=category.replace(" ","").trim();
        int totalRecord=zy2Service.findTotalNum(category);
        Page<Zy2> coverPage = new Page<Zy2>(pageSize, page, totalRecord, null);
        int start = (coverPage.getCurrentPage() - 1) * pageSize;
        map.put("start", start);
        map.put("size", pageSize);
        map.put("category",category);
        List<Banner> banners=new ArrayList<Banner>();
        banners=query_360.findBner();
        List<Zy2> list=new ArrayList<Zy2>();
        list=zy2Service.findZy2ByCategory(map);
        model.addAttribute("listCover",list);
        model.addAttribute("baseurl","3");
        model.addAttribute("banners",banners);
        model.addAttribute("totalRecord",totalRecord);
        model.addAttribute("pageUrl","zy2_category.do?category="+category+"&page=");
        model.addAttribute("curr",page);
        return "index";
    }

    @RequestMapping("/zy2_img")
    public String findVideoByImg(String image,Model model){
        Zy2 video=new Zy2();
        video=zy2Service.findZy2ByImg(image);
        model.addAttribute("video",video);
        Map<String,String> map_url=new LinkedHashMap<String, String>();
        Map<String,String> map_url1=new LinkedHashMap<String, String>();
        Map<String,String> map_url2=new LinkedHashMap<String, String>();
        String arr[]=video.getUrl().split(",");
        String first_url="";
        String first_url1="";
        String first_url2="";
        boolean first1_flag=true;
        boolean first2_flag=true;
        for(int i=0;i<arr.length;i++){
            if(arr[i].contains(".m3u8")){
                int index = arr[i].indexOf("$");
                map_url1.put(arr[i].substring(0, index),arr[i].substring(index + 1));
                if(first1_flag){
                    first_url1=arr[i].substring(index + 1);
                    first1_flag=false;
                }
            }
            if(arr[i].contains("share")){
                int index = arr[i].indexOf("$");
                map_url2.put(arr[i].substring(0, index),arr[i].substring(index + 1));
                if(first2_flag){
                    first_url2=arr[i].substring(index + 1);
                    first2_flag=false;
                }
            }
        }
        if(map_url1.size()>=map_url2.size()){
            map_url=map_url1;
            first_url=first_url1;
        }
        else {
            map_url=map_url2;
            first_url=first_url2;
        }
        model.addAttribute("first",first_url);
        model.addAttribute("map_url",map_url);
        model.addAttribute("zyk","1");
        return "to_player";
    }
    @ResponseBody
    @RequestMapping("/zy2_img2")
    public Zy2 findVideoByImg(String image){
        Zy2 video=new Zy2();
        video=zy2Service.findZy2ByImg(image);
        Map<String,String> map_url=new LinkedHashMap<String, String>();
        String arr[]=video.getUrl().split(",");
        String str="";
        for(int i=0;i<arr.length;i++){
            if(arr[i].contains(".m3u8")){
                int index = arr[i].indexOf("_");
                str+=arr[i];
                map_url.put(arr[i].substring(0, index),arr[i].substring(index + 1));
            }

        }
        video.setUrl(str);
        return video;
    }


}
